﻿using System;
using System.Text;
using System.Windows;
using BaMusaSoft.RPS.ViewModel.Properties;
using GalaSoft.MvvmLight.Messaging;

namespace BaMusaSoft.RPS.ViewModel.HelperClasses
{
    public static class Helper
    {
        internal static string AddDateMask(string dateString)
        {
            if (string.IsNullOrEmpty(dateString)) throw new ArgumentNullException("dateString");
            if (dateString.Length != 8) throw new FormatException("Not recognizable date string format");
            foreach (char c in dateString)
            {
                if (!Char.IsDigit(c)) throw new FormatException("Date string can only contain digits.");
            }
            string y = dateString.Substring(0, 4);
            string m = dateString.Substring(4, 2);
            string d = dateString.Substring(6, 2);
            string f = y + "/" + m + "/" + d;
            return f;
        }


        internal static string GenerateOutboxNo(string maxNo)
        {
            if (string.IsNullOrEmpty(maxNo)) throw new ArgumentNullException("maxNo");
            int incrementedNo;
            if (int.TryParse(maxNo, out incrementedNo))
            {
                incrementedNo++;
            }
            return incrementedNo.ToString();
        }

        /// <summary>
        /// Determines whether the specified date string is a valid date.
        /// </summary>
        /// <param name="dateString">The date string.</param>
        /// <returns>
        ///   <c>true</c> if [is valid date] [the specified date string]; otherwise, <c>false</c>.
        /// </returns>
        internal static bool IsValidDate(string dateString)
        {
            bool result = false;
            if (String.IsNullOrEmpty(dateString))
            {
                return false;
            }
            if (dateString.Length != 8)
            {
                return false;
            }
            string d = dateString.Substring(6, 2);
            string m = dateString.Substring(4, 2);
            string y = dateString.Substring(0, 4);

            int day;
            int month;
            int year;

            if (Int32.TryParse(d, out day) &&
                Int32.TryParse(m, out month) &&
                Int32.TryParse(y, out year))
            {
                if (
                    (day > 0 && day < 31)
                    &&
                    (month > 0 && month <= 12)
                    &&
                    (year > 1400))
                {
                    result = true;
                }
            }
            return result;
        }

        internal static int StartNewIncrement(string currentYear)
        {
            int temp;
            if (!int.TryParse(currentYear, out temp)) // check for invalid year 
            {
                throw new InvalidCastException("Current Year must be number only");
            }
            string s = currentYear + "0001";
            return int.Parse(s);
        }

        /// <summary>
        /// Ask the subscriber for confirmation the a given message.
        /// </summary>
        /// <param name="msg">The confirmation message.</param>
        /// <param name="token">The token. This to reporesent a unique id of the sender.</param>
        /// <returns>True if subscriber confirmed otherwise false.</returns>
        internal static bool MessageConfirmed(string msg, object token)
        {
            bool confirmed = false;
            var d = new DialogMessage(msg, result =>
                                               {
                                                   if (result == MessageBoxResult.Yes)
                                                   {
                                                       confirmed = true;
                                                   }
                                               })
                        {
                            Button = MessageBoxButton.YesNo,
                            Caption = Resources.ApplicationCaption,
                            Icon = MessageBoxImage.Question,
                            Options = MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading,
                            DefaultResult = MessageBoxResult.No
                        };
            Messenger.Default.Send(d, token);
            return confirmed;
        }

        /// <summary>
        /// Gets all exception messages in the supplied exception.
        /// </summary>
        /// <param name="ex">The exception.</param>
        /// <returns></returns>
        internal static string GetAllExceptionMessages(Exception ex)
        {
            var sb = new StringBuilder();
            return GetAllExceptionMessages(ex, sb);
        }

        private static string GetAllExceptionMessages(Exception exception, StringBuilder sb)
        {
            if (null == sb) throw new ArgumentNullException("sb");
            if (exception != null)
            {
                sb.AppendLine(exception.Message);
                GetAllExceptionMessages(exception.InnerException, sb);
            }
            return sb.ToString();
        }
        public static string PutMask(string dateString)
        {
            if (!string.IsNullOrEmpty(dateString))
            {
                string y = dateString.Substring(0, 4);
                string m = dateString.Substring(4, 2);
                string d = dateString.Substring(6, 2);
                string f = y + "/" + m + "/" + d;
                return f;
            }
            return "";

        }
        public static bool TryUnmaskDate(string value, out string result)
        {

            if (value.Length != 12)
            {
                result = null;
                return false;
            }

            string y = value.Substring(value.Length - 4, 4);
            string m = value.Substring(value.Length - 8, 2);
            string d = value.Substring(0, 2);
            string unmaskedDate = y + m + d;
            result = unmaskedDate;
            return true;

        }
        /// <summary>
        /// This will ensure that year string is in range of 1432 to 1500
        /// </summary>
        /// <param name="currentYear"></param>
        /// <returns></returns>
        public static bool YearInRange(string currentYear)
        {
            if(string.IsNullOrEmpty(currentYear)) throw new ArgumentNullException("currentYear");
            int year;
            if(! int.TryParse(currentYear, out year))
            {
                throw new InvalidOperationException("supplied string in not an integer");
            }
            bool result = false;
            if (year >= 1432 && year <= 9999)
            {
                result = true;
            }
            return result;

        }
    }
}